//
//  KFDNavBarTab.m
//  KFD
//
//  Created by JIRENTIANXIANG on 6/30/15.
//  Copyright © 2015 kfd. All rights reserved.
//

#import "KFDNavBarTab.h"
#import "ui_config.h"

@interface KFDNavBarTab ()

@property (strong,nonatomic) UIButton* goldButton;
@property (strong,nonatomic) UIButton* silverButton;
@property (strong,nonatomic) UIView* slider;

@end

@implementation KFDNavBarTab

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        CGSize size=frame.size;
        
        self.goldButton=[UIButton buttonWithType:UIButtonTypeSystem];
        self.goldButton.frame=CGRectMake(0, 0, 100, size.height);
        self.goldButton.center=CGPointMake(size.width/2-50, size.height/2);
        [self.goldButton setTitle:@"黄金ETF" forState:UIControlStateNormal];
        self.goldButton.titleLabel.textAlignment=NSTextAlignmentCenter;
        self.goldButton.titleLabel.textColor=KFD_WHITE;
        self.goldButton.titleLabel.font=[UIFont boldSystemFontOfSize:19];
        [self.goldButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        self.goldButton.tag=1;
        
        self.silverButton=[UIButton buttonWithType:UIButtonTypeSystem];
        self.silverButton.frame=CGRectMake(0, 0, 100, size.height);
        self.silverButton.center=CGPointMake(size.width/2+50, size.height/2);
        [self.silverButton setTitle:@"白银ETF" forState:UIControlStateNormal];
        self.silverButton.titleLabel.textAlignment=NSTextAlignmentCenter;
        self.silverButton.titleLabel.textColor=KFDColorRGB(0xee, 0xbb, 0xbf);
        self.silverButton.titleLabel.font=[UIFont systemFontOfSize:16];
        [self.silverButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        self.silverButton.tag=2;
        
        self.slider=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 3)];
        self.slider.center=CGPointMake(self.goldButton.center.x, 32);
        self.slider.backgroundColor=KFD_WHITE;
        
        [self addSubview:self.goldButton];
        [self addSubview:self.silverButton];
        [self addSubview:self.slider];
    }
    return self;
}



-(void)buttonPressed:(UIButton*)sender
{
    __weak typeof(self) weakSelf=self;
    if (self.didSelectTypeAction) {
        weakSelf.didSelectTypeAction(sender.tag);
    }
    UIButton* unSelectedButton=sender==self.goldButton?self.silverButton:self.goldButton;
    [UIView animateWithDuration:0.4 animations:^{
        sender.titleLabel.textColor=KFD_WHITE;
        sender.titleLabel.font=[UIFont boldSystemFontOfSize:19];
        unSelectedButton.titleLabel.textColor=KFDColorRGB(0xee, 0xbb, 0xbf);
        unSelectedButton.titleLabel.font=[UIFont systemFontOfSize:16];
        self.slider.center=CGPointMake(sender.center.x, 32);
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
